// pages/buycar/buycar.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: "",
    add: "",
    imgsrv: app.globalData.srvlink + '/diancan/img/',
    lists: '',
    wid: ''
  },
  onLoad: function (s) {
    var icon = app.globalData.joinCar

    if (icon.length == 0) {
      this.setData({
        imgUrl: "../../imgs/car.png",
        add: "去添加点什么吧",
        wid: '0',
        lists: ''
      });
    } else if (icon.length != 0) {
      this.setData({
        imgUrl: "",
        add: "",
        lists: icon,
        wid: "100%",
        hei: "100rpx",
      })
    }
  },
  wxpay: function (s) {
    console.log(app.globalData.joinCar);
    var allprice = 0;
    for (var i = 0; i < app.globalData.joinCar.length; i++) {
      allprice += parseFloat(app.globalData.joinCar[i].price1);
    }
    console.log("总价：" + allprice)
    wx.showModal({
      title: '提示',
      content: '是否进行微信支付？全部结算金额为：' + allprice + '元',
      success: function (res) {
        if (res.confirm) {
          var code = null;

          wx.login({
            success: function (res) {
              if (res.code) {
                code = res.code;
                console.log("点击支付按钮" + code);

                wx.request({
                  url: 'https://jredu.applinzi.com/wxpayapi/example/jsapi.php',
                  data: {
                    code: code,
                    parse: allprice,
                  },
                  method: 'GET',
                  header: {
                    'content-type': 'application/json'
                  },
                  success: function (res) {
                    var data = res.data;
                    wx.requestPayment({
                      timeStamp: data.timeStamp,
                      nonceStr: data.nonceStr,
                      package: data.package,
                      signType: 'MD5',
                      paySign: data.paySign,
                      success: function () {

                        console.log("支付成功");
                      },
                      complete: function () {
                        console.log("完成")
                        for (var i = 0; i < app.globalData.joinCar.length; i++) {
                          app.globalData.joinOrder.push(app.globalData.joinCar[i])
                        }
                        this.icon = [];
                        app.globalData.joinCar = []
                        //完成交易后跳转到订单



                      },
                      fail: function () {
                        console.log("支付失败")
                      }
                    })
                    console.log(data);
                  }
                })
              } else {
                console.log('获取用户登录态失败！' + res.errMsg)
              }
            }
          });
        } else {
          console.log('用户点击取消')

        }

      }
    })
  },


  deletePay: function (e) {
    wx.showModal({
      title: '删除提示',
      content: '您确定要删除吗？',
      success: function (res) {
        if (res.confirm) {
          app.globalData.joinCar.splice(e.currentTarget.id, 1);
          var page = getCurrentPages().pop();
          page.onLoad();
        } else {
          console.log("用户点击取消")
        }
      }
    })

  },
  onShow: function () {
    this.onLoad();
  },







})
